package com.example.netty.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

@Slf4j
public class Client {

    private static  int DEFAULT_SERVER_PORT =8888;
    private static  String DEFAULT_SERVER_IP ="127.0.0.1";

    public  static void
    send(String expression){
        send(DEFAULT_SERVER_PORT,expression);
    }

    private static void send(int port, String expression) {

        Socket socket =null;
        BufferedReader in =null;
        PrintWriter out =null;
        try {
            socket = new Socket(DEFAULT_SERVER_IP, port);
            in   = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            out.println(expression);


            System.out.println("服务器："+in.readLine());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getLocalizedMessage());
        }finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in =null;
            }
            if (out!=null){
                out.close();
                out=null;
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                socket=null;
            }
        }
    }

}
